package com.softlook.mt.server.db.dao;

import java.util.List;
import java.util.LinkedList;
import java.util.Hashtable;
import java.lang.Integer;
import java.lang.StringBuffer;
import jp.crossfire.framework.database.ICondition;
import java.sql.Connection;
import jp.crossfire.framework.database.SQLExecutor;
import com.softlook.mt.server.db.internal.DeviceEventManagerGetListner;
import jp.crossfire.framework.database.ConditionContainer;
import jp.crossfire.framework.database.Values;
import jp.crossfire.framework.database.Condition;
import com.softlook.mt.server.db.internal.DeviceEventManagerGetLastListner;
import com.softlook.mt.server.db.internal.DeviceEventManagerGetMaxDeviceEventIdListner;
import com.softlook.mt.server.db.internal.DeviceEventManagerInternal;

public class DeviceEventManager extends DeviceEventManagerInternal {
	private List deviceevent = new LinkedList();
	public List getDeviceevent() {
		return this.deviceevent;
	}
	public void setDeviceevent(List deviceevent) {
		this.deviceevent = deviceevent;
	}
	public String getSql(Hashtable input, Integer limit, Integer offset) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("SELECT ");
		buf.append("*");
		
		buf.append(" FROM ");
		buf.append("deviceevent");
		
		// Condition
		ICondition condition = getGetCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" WHERE " + condition.toString());
		}
		
		// GROUP BY
		
		// Having Condition
		condition = getGetHavingCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" HAVING " + condition.toString());
		}
		
		// ORDER
		
		// Limit
		if(limit != null && offset != null){
			buf.append(" LIMIT " + offset.toString() +", " + limit.toString());
		}
		
		return buf.toString();
	}
	public List get(Connection con, Hashtable input, Integer limit, Integer offset) throws jp.crossfire.framework.database.HyperDbException {
		this.reset();
		String sql = getSql(input, limit, offset);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		// Listners
		List listners = new LinkedList();
		
		DeviceEventManagerGetListner listner0 = (new DeviceEventManagerGetListner());
		listners.add(listner0);
		
		// Execute Query
		exec.executeSelectSQL(sql, listners);
		listner0.orMap();
		
		this.deviceevent = listner0.deviceeventTmpCache;
		return listner0.result;
	}
	public String addSql(Hashtable input) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("INSERT INTO ");
		buf.append("deviceevent");
		
		ConditionContainer cnd0= new Values("VALUES");
		ConditionContainer cnd1 = new ConditionContainer(",");
		ConditionContainer cnd2 = new ConditionContainer(",");
		cnd0.addChild(cnd1);
		cnd0.addChild(cnd2);
		Condition cnd3 = new Condition("gpsdataid");
		cnd1.addChild(cnd3);
		Condition cnd4 = new Condition("'$gpsdataid'");
		if(input.get("gpsdataid") != null){
			((Condition)cnd4).getProperties().put("gpsdataid", input.get("gpsdataid"));
		}
		cnd2.addChild(cnd4);
		Condition cnd5 = new Condition("deviceid");
		cnd1.addChild(cnd5);
		Condition cnd6 = new Condition("'$deviceid'");
		if(input.get("deviceid") != null){
			((Condition)cnd6).getProperties().put("deviceid", input.get("deviceid"));
		}
		cnd2.addChild(cnd6);
		Condition cnd7 = new Condition("eventid");
		cnd1.addChild(cnd7);
		Condition cnd8 = new Condition("'$eventid'");
		if(input.get("eventid") != null){
			((Condition)cnd8).getProperties().put("eventid", input.get("eventid"));
		}
		cnd2.addChild(cnd8);
		Condition cnd9 = new Condition("placeid");
		cnd1.addChild(cnd9);
		Condition cnd10 = new Condition("'$placeid'");
		if(input.get("placeid") != null){
			((Condition)cnd10).getProperties().put("placeid", input.get("placeid"));
		}
		cnd2.addChild(cnd10);
		Condition cnd11 = new Condition("time");
		cnd1.addChild(cnd11);
		Condition cnd12 = new Condition("'$time'");
		if(input.get("time") != null){
			((Condition)cnd12).getProperties().put("time", input.get("time"));
		}
		cnd2.addChild(cnd12);
		if(!cnd0.toString().equals("")){
			buf.append(" " + cnd0.toString());
		}
		
		return buf.toString();
	}
	public void add(Connection con, Hashtable input) throws jp.crossfire.framework.database.HyperDbException {
		String sql = addSql(input);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		exec.execUpdateSQL(sql);
	}
	public String getLastSql(Hashtable input, Integer limit, Integer offset) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("SELECT ");
		buf.append("*");
		
		buf.append(" FROM ");
		buf.append("deviceevent");
		
		// Condition
		ICondition condition = getLastGetCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" WHERE " + condition.toString());
		}
		
		// GROUP BY
		
		// Having Condition
		condition = getLastGetHavingCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" HAVING " + condition.toString());
		}
		
		// ORDER
		
		// Limit
		if(limit != null && offset != null){
			buf.append(" LIMIT " + offset.toString() +", " + limit.toString());
		}
		
		return buf.toString();
	}
	public List getLast(Connection con, Hashtable input, Integer limit, Integer offset) throws jp.crossfire.framework.database.HyperDbException {
		this.reset();
		String sql = getLastSql(input, limit, offset);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		// Listners
		List listners = new LinkedList();
		
		DeviceEventManagerGetLastListner listner0 = (new DeviceEventManagerGetLastListner());
		listners.add(listner0);
		
		// Execute Query
		exec.executeSelectSQL(sql, listners);
		listner0.orMap();
		
		this.deviceevent = listner0.deviceeventTmpCache;
		return listner0.result;
	}
	public String getMaxDeviceEventIdSql(Hashtable input, Integer limit, Integer offset) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("SELECT ");
		buf.append("max");
		buf.append("(");
		buf.append("deviceeventid");
		buf.append(")");
		
		buf.append(" FROM ");
		buf.append("deviceevent");
		
		// Condition
		ICondition condition = getMaxDeviceEventIdGetCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" WHERE " + condition.toString());
		}
		
		// GROUP BY
		
		// Having Condition
		condition = getMaxDeviceEventIdGetHavingCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" HAVING " + condition.toString());
		}
		
		// ORDER
		
		// Limit
		if(limit != null && offset != null){
			buf.append(" LIMIT " + offset.toString() +", " + limit.toString());
		}
		
		return buf.toString();
	}
	public List getMaxDeviceEventId(Connection con, Hashtable input, Integer limit, Integer offset) throws jp.crossfire.framework.database.HyperDbException {
		this.reset();
		String sql = getMaxDeviceEventIdSql(input, limit, offset);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		// Listners
		List listners = new LinkedList();
		
		DeviceEventManagerGetMaxDeviceEventIdListner listner0 = (new DeviceEventManagerGetMaxDeviceEventIdListner());
		listners.add(listner0);
		
		// Execute Query
		exec.executeSelectSQL(sql, listners);
		return listner0.result;
	}
	public void reset() {
			this.deviceevent.clear();
	}
}
